package com.lizhen.core.function.weblogic;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/*
 * @author lizhen
 * @Title Weblogic
 * @Description About Weblogic mechanism
 * cd ${weblogic_home}/wlserver/server/lib
 * java -jar wljarbuilder.jar
 * @Company com.lizhen
 */
public class Weblogic {

    private final static String INITIAL_CONTEXT_FACTORY = "weblogic.jndi.WLInitialContextFactory";
    private final static String PROVIDER_URL = "t3://localhost:7001";
    private final static String ORACLE_JNDI_NAME = "JNDI/OracleDataSource";
    private final static String MYSQL_JNDI_NAME = "JNDI/MySQLDataSource";
    private final static String Weblogic_User = "Weblogic";
    private final static String Weblogic_Pswd = "Lz211314";
    private final static String JNDI_PATH = "1.8/api/main/resource/com/lizhen/core/function/weblogic/Weblogic/jndi.properties";
    private static DataSource dsOracle = null;
    private static DataSource dsMySQL = null;

    public static void main(String[] args) {
        Weblogic weblogic = new Weblogic();
        weblogic.findPerson();
    }

    public void findPerson() {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
/*            Properties properties = new Properties();
            properties.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
            properties.put(Context.PROVIDER_URL, PROVIDER_URL);
            InitialContext ctx = new InitialContext(properties);*/
/*            System.setProperty(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
            System.setProperty(Context.PROVIDER_URL, PROVIDER_URL);
            InitialContext ctx = new InitialContext();*/
            Properties properties = new Properties();
            properties.load(new FileInputStream(JNDI_PATH));
            InitialContext ctx = new InitialContext(properties);
            dsOracle = (DataSource) ctx.lookup(ORACLE_JNDI_NAME);
            dsMySQL = (DataSource) ctx.lookup(MYSQL_JNDI_NAME);
            String sql = "select name,age from person";
            /*conn = dsOracle.getConnection(Weblogic_User, Weblogic_Pswd);*/
            conn = dsOracle.getConnection();
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);

            while (rs.next()) {
                System.out.println("用户名：" + rs.getString(1) + " 密码：" + rs.getInt(2));
            }

            /*conn = dsMySQL.getConnection(Weblogic_User, Weblogic_Pswd);*/
            conn = dsMySQL.getConnection();
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);

            while (rs.next()) {
                System.out.println("用户名：" + rs.getString(1) + " 密码：" + rs.getInt(2));
            }

            rs.close();
            stmt.close();
            conn.close();
        } catch (NamingException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
